package org.jeecg.modules.basic.warehouse.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.config.JeecgBaseConfig;
import org.jeecg.modules.basic.warehouse.entity.WmsBasicWarehouseArea;
import org.jeecg.modules.basic.warehouse.entity.WmsBasicWarehouseAreaLocation;
import org.jeecg.modules.basic.warehouse.service.IWmsBasicWarehouseAreaLocationService;
import org.jeecg.modules.basic.warehouse.service.IWmsBasicWarehouseAreaService;
import org.jeecg.modules.business.repertory.entity.WmsRepertory;
import org.jeecg.modules.business.repertory.service.IWmsRepertoryService;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 仓库-库区
 * @Author: jeecg-boot
 * @Date: 2023-08-09
 * @Version: V1.0
 */
@Api(tags = "仓库-库区")
@RestController
@RequestMapping("/basic.warehouse/wmsBasicWarehouseArea")
@Slf4j
public class WmsBasicWarehouseAreaController extends JeecgController<WmsBasicWarehouseArea, IWmsBasicWarehouseAreaService> {
    @Resource
    private IWmsBasicWarehouseAreaService wmsBasicWarehouseAreaService;
    @Autowired
    private IWmsRepertoryService repertoryService;
    @Autowired
    private IWmsBasicWarehouseAreaLocationService wmsBasicWarehouseAreaLocationService;
    @Resource
    private JeecgBaseConfig jeecgBaseConfig;

    /**
     * 分页列表查询
     *
     * @param wmsBasicWarehouseArea
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "仓库-库区-分页列表查询")
    @ApiOperation(value = "仓库-库区-分页列表查询", notes = "仓库-库区-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<WmsBasicWarehouseArea>> queryPageList(WmsBasicWarehouseArea wmsBasicWarehouseArea,
                                                              @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                              @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                              HttpServletRequest req) {
        QueryWrapper<WmsBasicWarehouseArea> queryWrapper = QueryGenerator.initQueryWrapper(wmsBasicWarehouseArea, req.getParameterMap());
        Page<WmsBasicWarehouseArea> page = new Page<WmsBasicWarehouseArea>(pageNo, pageSize);
        IPage<WmsBasicWarehouseArea> pageList = wmsBasicWarehouseAreaService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param wmsBasicWarehouseArea
     * @return
     */
    @AutoLog(value = "仓库-库区-添加")
    @ApiOperation(value = "仓库-库区-添加", notes = "仓库-库区-添加")
    @RequiresPermissions("basic.warehouse:wms_basic_warehouse_area:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody WmsBasicWarehouseArea wmsBasicWarehouseArea) {
        wmsBasicWarehouseAreaService.saveByCheck(wmsBasicWarehouseArea);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param wmsBasicWarehouseArea
     * @return
     */
    @AutoLog(value = "仓库-库区-编辑")
    @ApiOperation(value = "仓库-库区-编辑", notes = "仓库-库区-编辑")
    @RequiresPermissions("basic.warehouse:wms_basic_warehouse_area:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody WmsBasicWarehouseArea wmsBasicWarehouseArea) {
        wmsBasicWarehouseAreaService.updateByCheck(wmsBasicWarehouseArea);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "仓库-库区-通过id删除")
    @ApiOperation(value = "仓库-库区-通过id删除", notes = "仓库-库区-通过id删除")
    @RequiresPermissions("basic.warehouse:wms_basic_warehouse_area:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        long isUse = wmsBasicWarehouseAreaLocationService.count(new LambdaQueryWrapper<WmsBasicWarehouseAreaLocation>()
                .eq(WmsBasicWarehouseAreaLocation::getWmsBasicWarehouseAreaId, id)
                .eq(WmsBasicWarehouseAreaLocation::getStatus, "1"));
        if (isUse > 0) {
            throw new JeecgBootException("该库区存在启用的库位数据, 不能删除!");
        }
        long count = repertoryService.count(new LambdaQueryWrapper<WmsRepertory>()
                .eq(WmsRepertory::getWmsBasicWarehouseAreaId, id)
                .ge(WmsRepertory::getStockQuantity, 0));
        if (count > 0) {
            throw new JeecgBootException("该库区有即时库存数据，不能删除！");
        }
        wmsBasicWarehouseAreaService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "仓库-库区-批量删除")
    @ApiOperation(value = "仓库-库区-批量删除", notes = "仓库-库区-批量删除")
    @RequiresPermissions("basic.warehouse:wms_basic_warehouse_area:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.wmsBasicWarehouseAreaService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "仓库-库区-通过id查询")
    @ApiOperation(value = "仓库-库区-通过id查询", notes = "仓库-库区-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<WmsBasicWarehouseArea> queryById(@RequestParam(name = "id", required = true) String id) {
        WmsBasicWarehouseArea wmsBasicWarehouseArea = wmsBasicWarehouseAreaService.getById(id);
        if (wmsBasicWarehouseArea == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(wmsBasicWarehouseArea);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param wmsBasicWarehouseArea
     */
    @ApiOperation(value = "导出excel", notes = "导出excel")
    @RequiresPermissions("basic.warehouse:wms_basic_warehouse_area:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, WmsBasicWarehouseArea wmsBasicWarehouseArea) {
        return super.exportXls(request, wmsBasicWarehouseArea, WmsBasicWarehouseArea.class, "仓库-库区");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @ApiOperation(value = "通过excel导入数据", notes = "通过excel导入数据")
    @RequiresPermissions("basic.warehouse:wms_basic_warehouse_area:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, WmsBasicWarehouseArea.class);
    }

}
